package battleship.cxbr;

import java.util.ArrayList;

import battleship.emss.*;

public class WanderContext extends Context
{
	protected int x;
	protected int y;
	
	public WanderContext(String n,CxBRAgent a)
	{
		super(n,a);
	}
	
	//How to get here from above
	public double ParentRule(double Time)
	{
		
		return 0.0;
	}
	//How to get here from side
	public double SiblingRule(double Time)
	{
		return 0.3;
	}
	
	//Things to do when just entering
	public void Initialize(double Time)
	{
		x=(int)(Math.random()*900+50);
		y=(int)(Math.random()*900+50);
		//x=600;
		//y=10;
	}

	
	//What do actually do
	public void Action(double Time)
	{
		Agent.status.status=Name+" "+x+" "+y;
		Agent.actions.clear();
		
		//driving, and got there, go somewhere else
		if(Math.sqrt(Math.pow(Agent.status.x-x,2)+Math.pow(Agent.status.y-y,2))<5)
		{
			x=(int)(Math.random()*900+50);
			y=(int)(Math.random()*900+50);
		}
		
		Agent.actions.add(new Move(Agent.status.x,Agent.status.y,x,y,6));
		
		//detected others
		for(int i=0;i<Agent.detectedStatus.size();i++)
		{
			Status tmpStatus=Agent.detectedStatus.get(i);
			//not on my team, and a sub
			//And I am close enough
			//and it is alive
			
			if(tmpStatus.team!=Agent.status.team && tmpStatus.type==Agent.SUB && tmpStatus.alive==1)
			{
				Agent.status.status+=" Detect";
				//less than 15 miles off
				if(Math.sqrt(Math.pow(Agent.status.mx-tmpStatus.mx,2)+Math.pow(Agent.status.my-tmpStatus.my,2))<15*1600)
				{
					Agent.actions.add(new Fire(Agent.status.mx,Agent.status.my,tmpStatus.mx,tmpStatus.my));
				}
			}
		}
		
		//Receives communications communications
		//might want to transition contexts
		if(Agent.incomingMessages.size()>0)
		{
			for(int i=0;i<Agent.incomingMessages.size();i++)
			{
				if(Agent.incomingMessages.get(i).message=="Under Attack")
				{
					Agent.status.status+=" "+"M:Carrier being Attacked";
				}
			}
		}
		Agent.incomingMessages.clear();
		
				
		return;
	}
	
}

